package com.yuxinmuyu.sports_management.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuxinmuyu.sports_management.bean.entiy.STeacher;
import com.yuxinmuyu.sports_management.bean.request.SelectTeacherRequest;
import com.yuxinmuyu.sports_management.bean.request.TeacherIDRequest;
import com.yuxinmuyu.sports_management.bean.response.STeacherResponse;
import com.yuxinmuyu.sports_management.exception.BusinessException;
import com.yuxinmuyu.sports_management.mapper.STeacherMapper;
import com.yuxinmuyu.sports_management.service.STeacherService;
import com.yuxinmuyu.sports_management.utils.UserUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

@Service
public class STeacherServiceImpl implements STeacherService {
    @Resource
    private STeacherMapper sTeacherMapper;
    @Override
    public Page<STeacherResponse> selectTeachers(SelectTeacherRequest request) {
        Page<STeacherResponse> page = new Page<>(request.getCurrentPage(), request.getPageSize());
        page =sTeacherMapper.selectTeachers(page, request.getDeptID(), request.getKeyword());
        return page;
    }

    @Transactional( rollbackFor = Exception.class)
    @Override
    public void deleteTeacher(TeacherIDRequest request) {
        int n=sTeacherMapper.deleteById(request.getTeacherID());
        if(n==0){
            throw new BusinessException("删除ID为"+request.getTeacherID()+"的项目信息失败!");
        }
    }

    @Override
    public void addTeacher(STeacher teacher) {
        STeacher sTeacher = sTeacherMapper.selectById(teacher.getTeacherID());
        if(Objects.nonNull(sTeacher)){
            throw new BusinessException("已经存在该信息！");
        }
        teacher.setOperator(UserUtil.getUserName());
        teacher.setLastDate(new Date());
        int n = sTeacherMapper.insert(teacher);
        if (n==0){
            throw new BusinessException("添加失败！");
        }
    }

    @Transactional( rollbackFor = Exception.class)
    @Override
    public void updateTeacher(STeacher teacher) {
       int n = sTeacherMapper.update(null, Wrappers.<STeacher>lambdaUpdate().eq(
                       STeacher::getTeacherID,teacher.getTeacherID())
               .set(STeacher::getDeptID,teacher.getDeptID())
               .set(STeacher::getTeacherName,teacher.getTeacherName())
               .set(STeacher::getEnteryDate,teacher.getEnteryDate())
               .set(STeacher::getTeacherBirth,teacher.getTeacherBirth())
               .set(STeacher::getTeacherSex,teacher.getTeacherSex())
               .set(STeacher::getPhoneNumber,teacher.getPhoneNumber())
               .set(STeacher::getEmail,teacher.getEmail())
               .set(STeacher::getLastDate,new Date())
               .set(STeacher::getOperator,UserUtil.getUserName())
       );
        if (n==0){
            throw new BusinessException("修改失败！");
        }
    }



}
